package com.shengde.jinwawaforleader.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.shengde.jinwawaforleader.HomeApplication;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.fragment.HomePageFragment;
import com.shengde.jinwawaforleader.fragment.MyJobFragment;
import com.shengde.jinwawaforleader.fragment.PersonalCenterFragment;
import com.shengde.jinwawaforleader.model.myJob.instance_message.DemoHelper;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProVersionUpdate;
import com.shengde.jinwawaforleader.views.CustomShowDialog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MainActivity extends BaseActivity {
	private static final String TAG = "MainActivity";
	//按返回键按了几次
	private int pressCount;
	private RadioButton home_page;
	private RadioButton my_job;
	private RadioButton personal_center;
	private RadioGroup rg_content;
	private HomePageFragment hp_fragment;
	private MyJobFragment mj_fragment;
	private PersonalCenterFragment pc_fragment;

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main);


	}

	@Override
	public void initView() {
		home_page = (RadioButton) findViewById(R.id.home_page);
		my_job = (RadioButton) findViewById(R.id.my_job);
		personal_center = (RadioButton) findViewById(R.id.personal_center);
		rg_content = (RadioGroup) findViewById(R.id.rg_content);
	}

	@Override
	public void initData() {
		checkUpdate(this);

		DemoHelper.getInstance().setCurrentUserName(User.getUserPhone(this));
		EMClient.getInstance().login(User.getUserPhone(MainActivity.this), "123456", new EMCallBack() {//回调
			@Override
			public void onSuccess() {
				EMClient.getInstance().groupManager().loadAllGroups();
				EMClient.getInstance().chatManager().loadAllConversations();
				Log.i(TAG, "登录聊天服务器成功！");
			}

			@Override
			public void onProgress(int progress, String status) {

			}

			@Override
			public void onError(int code, String message) {
				Log.i(TAG, "登录聊天服务器失败！" + code + message);
				Toast.makeText(MainActivity.this, "登录聊天服务器失败", Toast.LENGTH_SHORT).show();
			}
		});
		mj_fragment = new MyJobFragment();
		pc_fragment = new PersonalCenterFragment();
		hp_fragment = new HomePageFragment();
		FragmentManager manager = getFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.fl_content, hp_fragment);
		transaction.commit();
	}

	@Override
	public void initListener() {
		rg_content.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int i) {
				FragmentManager manager = getFragmentManager();
				FragmentTransaction transaction = manager.beginTransaction();
				if (i == home_page.getId()) {
					transaction.replace(R.id.fl_content, hp_fragment);
				} else if (i == my_job.getId()) {
					transaction.replace(R.id.fl_content, mj_fragment);
				} else if (i == personal_center.getId()) {
					transaction.replace(R.id.fl_content, pc_fragment);
				}
				transaction.commit();
			}
		});
	}

	@Override
	public void onWidgetClick(View view) {

	}

	@Override
	public void onBackPressed() {
		pressCount++;
		if (pressCount == 1) {
			HomeApplication.showToastShort(getString(R.string.exit_kingvar));
		} else if (pressCount == 2) {
			finish();
		}
		new Thread() {
			public void run() {
				SystemClock.sleep(2000);
				pressCount = 0;
			}
		}.start();
	}


	public static int getAppVersionCode(Context context) {
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			return pi.versionCode;

		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static void checkUpdate(final Activity act) {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/public/VertionUpdate";
		} else {
			url = BaseProtocol.HOST_BASE + "/public/VertionUpdate";
		}
		RequestParams params = BaseProtocol.getRequestParams(url);
		params.addParameter("apptype", "0");
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProVersionUpdate update = new Gson().fromJson(result, ProVersionUpdate.class);
				if (update != null && update.getCode() == 0) {
					int serverVerstion = update.getVertion();//新
					int curVersion = getAppVersionCode(act);//老
					Log.i(TAG, "onSuccess: serverVerstion = " + serverVerstion);
					Log.i(TAG, "onSuccess: curVersion = " + curVersion);
					Log.i(TAG, "onSuccess: path = " + update.getPath());
					PersonalCenterFragment.cc(serverVerstion);
					if (serverVerstion <= curVersion) {
						return;
					} else {
						Log.i(TAG, "onSuccess: getContent = " + update.getContent());
						CustomShowDialog.ShowCustomDialog(act, update.getContent(), update.getPath(), 0);
					}
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
			}

			@Override
			public void onCancelled(CancelledException cex) {
			}

			@Override
			public void onFinished() {
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}